
const inp=document.querySelector('.nav>.box>input')
const ul=document.querySelector('.nav>.box>ul')
const list2=document.querySelector(".list2")
const mulu=document.querySelector(".mulu")
const imgs=document.querySelectorAll('.picture>li')


const title=document.querySelector(".list2>.left>ul")
title.addEventListener('click',()=>
  window.location.href="../views/list.html"
)




mulu.onmouseover=function(){
    list2.classList.add("active")
}
list2.onmouseover=function(){
    list2.classList.add("active")
}
mulu.onmouseleave=function(){
    list2.classList.remove("active")
}
list2.onmouseleave=function(){
    list2.classList.remove("active")
}
inp.oninput = function () {
    const text = this.value.trim()
    const script = document.createElement('script')
    script.src = `https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=34305,34099,33970,34273,34278,34004,34281,34107,34111,26350,22159&wd=${ text }&req=2&csor=2&cb=fn&_=1627023610785`
    document.body.appendChild(script)
    script.remove()
  }

function fn(res) {
    if (!res.g) {
      ul.classList.remove('active')
      return
    }
    let str = ''
    res.g.forEach(item => {
      str += `<li>${ item.q }</li>`
    })
    ul.innerHTML = str
    ul.classList.add('active')
  }
  const listbox=document.querySelector('.picture>.listbox')
  $.ajax({
    url:'https://www.ikea.cn/api-search/cn/zh/product-list-page?',
    data:'c=cnweb&v=20210322&size=25&category=fu003',
    dataType:'json',
    success(res){
    console.log(res)
    const top=document.documentElement.scrollTop||document.body.scrollTop
    
    let str=''
    res.productListPage.productWindow.forEach(item=>{
    str += ` 
    <li>
        <img src="${item.mainImageUrl}" alt="">
        <p>${item.typeName}</p> 
        <div>
          <p class="price">￥<span>${item.priceNumeral}</span></p>
          <i>销量</i>
        </div>
    </li>
    `
    })
    listbox.innerHTML=str
   } 
  
 })
 const denglu=document.querySelector(".nav>.denglu")
 const cart=document.querySelector(".nav>.cart")
 cart.addEventListener('click',()=>window.open("../views/cart.html"))
 denglu.addEventListener('click',()=>window.open("../views/login.html"))

 const onBox = document.querySelector('.on')
 const offBox = document.querySelector('.off')
 const nameBox = onBox.querySelector('.username')
 const selfBtn = document.querySelector('.self')
 const id = window.localStorage.getItem('id')

testLogin()
function testLogin() {
  const login = window.localStorage.getItem('login')
  if (login !== '1') {
    offBox.classList.add('active')
    onBox.classList.remove('active')
    return
  }

  const token = window.localStorage.getItem('token')
  const xhr = new XMLHttpRequest()
  xhr.open('GET', 'http://localhost:8888/users/info?id=' + id)
  xhr.onload = () => testHandler(xhr)
  xhr.setRequestHeader('authorization', token)
  xhr.send()
}
function testHandler(xhr) {
  const res = JSON.parse(xhr.responseText)
  console.log(res)
  if (res.code !== 1) {
    offBox.classList.add('active')
    onBox.classList.remove('active')
    denglu.classList.remove('active')
    return
  }
  nameBox.innerText = res.info.nickname
  onBox.classList.add('active')
  offBox.classList.remove('active')
  denglu.classList.add('active')

}

const logoutBtn = document.querySelector('.logout')
logoutBtn.addEventListener('click', () => {
  ajax({
    url: 'http://localhost:8888/users/logout',
    data: `id=${ id }`,
    dataType: 'json',
    success: function (res) {
      if (res.code === 1) window.location.reload()
    }
  })
})


  